<?php
!defined('P_W') && exit('Forbidden');
require_once MODE . '/base/frontcontroller.php';

class SaleController extends FrontController {
	var $_layoutFile = 'sale';
	var $_baseUrl = 'index.php?m=house&q=sale';
	var $_houseSaleHelper;
	var $_seopage;
	var $_defaultSeo;
	var $_seoname;
	var $_num = 10;
	function _before(){
		parent::_before();
		$this->_houseSaleHelper = House::LoadClass('housesalehelper', 'helper');
		$this->_viewer->current['sale'] = 'class="current"';
		return true;
	}
	function run() {
		$this->_viewer->url = $this->_baseUrl;
		$hview = House::LoadClass('houseview', 'utility');//加载houseview类
		list($page, $keywords,  $houseids, $act,$options) = $this->_gp(array('page', 'keywords', 'houseids', 'act', 'options'));//关键字和排序方式
		$this->_viewer->act = !$act ? 'all' : $act;
		$this->_viewer->baseUrl = $hview->getBaseUrl($this->_baseUrl, $this->_viewer->act, $options, $keywords);
		
		$params = $hview->getParams($options);//获取参数
		$this->_viewer->options = $hview->getSaleOptionUrl($this->_baseUrl . '&act='.$act, $options);//获取URL数组
		$this->_viewer->isHouseManager = isHouseManager($GLOBALS['winduid'],'sale');
		$sortlist = $this->_getSortList();//排序方式
		$this->_viewer->sortlist = $sortlist;
		list($areatype, $platetype, $acreagetype, $pricetype, $roomtype, $housetype, $sort) = array($params['areatype'], $params['platetype'], $params['acreagetype'], $params['pricetype'], $params['roomtype'], $params['housetype'], $params['sort']);//接收参数
		
		$this->_viewer->areatype = (int)$areatype;
		$this->_viewer->platetype = (int)$platetype;
		$this->_viewer->acreagetype = (int)$acreagetype;
		$this->_viewer->pricetype = (int)$pricetype;
		$this->_viewer->roomtype = (int)$roomtype;
		$this->_viewer->housetype = (int)$housetype;		
		
		$expend = array();	
		($act == 'urgent') && $expend['isurgent'] = 1;
		($act == 'personal') && $expend['isbroker'] = 0;
		$this->_viewer->sort = $sort;
		$sort && list($sortField, $sortType) = explode(',',$sort);
		$saleService = House::LoadService('SecondSaleService');
		$this->_viewer->area = $saleService->getFieldsByType('area');//区域
		$this->_viewer->plate = $saleService->getPlateByAreaId($this->_viewer->areatype);
		$this->_viewer->price = $saleService->getFieldsByType('sellprice');//出售价格区间
		$this->_viewer->acreage = $saleService->getFieldsByType('acreage');//面积区间
		$this->_viewer->house = $saleService->getFieldsByType('property');//房屋类型区间		
		$this->_viewer->room = $saleService->roomType;//居室
		$arrayIndex = array('areatype'=>(int)$this->_viewer->areatype, 'platetype'=>(int)$this->_viewer->platetype,	'pricetype'=>(int)$this->_viewer->pricetype, 'acreagetype'=>(int)$this->_viewer->acreagetype, 'roomtype'=>(int)$this->_viewer->roomtype, 'housetype'=>(int)$this->_viewer->housetype);
		$condition = array();//搜索条件	
		$this->_viewer->areatype && $condition['areatype'] = $this->_viewer->area[$this->_viewer->areatype];
		$this->_viewer->platetype && $condition['platetype'] = $this->_viewer->plate[$this->_viewer->platetype];
		$this->_viewer->pricetype && $condition['pricetype'] = $this->_viewer->price[$this->_viewer->pricetype];
		$this->_viewer->acreagetype && $condition['acreagetype'] = $this->_viewer->acreage[$this->_viewer->acreagetype];
		$this->_viewer->roomtype && $condition['roomtype'] = $this->_viewer->room[$this->_viewer->roomtype];
		$this->_viewer->housetype && $condition['housetype'] = $this->_viewer->house[$this->_viewer->housetype];
		$this->_viewer->condition = $condition;
		($this->_viewer->act == 'all') && $this->_viewer->toplist = $saleService->getSecondInfos($hview->getTopHouseids('sale', $arrayIndex));
		$page = (intval($page) == 0) ? 1 : intval($page);
		list($this->_viewer->total, $this->_viewer->result) = $saleService->getDetailList($arrayIndex, $sortType, $sortField,$keywords, $page, $this->_num, $expend);
		if($this->_viewer->act == 'all' && !empty($this->_viewer->toplist)){
			foreach($this->_viewer->toplist as $value){
				$value['showTop'] = 1;
				array_unshift($this->_viewer->result, $value);				
			}
		}
		$this->_viewer->pages = numofpage($this->_viewer->total, $page, ceil($this->_viewer->total / $this->_num), $this->_viewer->baseUrl);//分页信息
		$seo = $this->_houseSaleHelper->setSeoParams('index');
		list($this->_seopage,$this->_defaultSeo,$this->_seoname) = $seo;

	}
	function ajax(){
		define('AJAX','1');
		list($part, $houseids) = $this->_gp(array('part','houseids'));
		if(in_array($part,array('report')) ){
			global $_G; 
			!$_G['allowreport'] && Error::showError('您没有使用举报的权限');
		}else{
			if (!isHouseManager($this->_getGlobal('winduid'),'sale')) Error::showError('您没有权限进入该页面',1); 
		}
		if(!$houseids){
			echo '未选择要操作的项!';
			ajax_footer();	
		}
		include(D_P.'data/bbscache/house_config.php');
		include(R_P.'require/credit.php');
		$this->_viewer->house_sale_rapidprice = $house_sale_rapidprice;
		$this->_viewer->house_sale_rapidcredit = $credit->cType[$house_sale_rapidcredit];
		
		$this->_viewer->house_sale_topprice = $house_sale_topprice;
		$this->_viewer->house_sale_topcredit = $credit->cType[$house_sale_topcredit];
		$act = array('urgent'=>$this->_baseUrl . '&a=urgent' . '&houseids=' . $houseids,
					 'top'=>$this->_baseUrl . '&a=top'. '&houseids=' . $houseids,
					 'delete'=>$this->_baseUrl . '&a=delete'. '&houseids=' . $houseids,
					 'report'=>$this->_baseUrl . '&a=report'. '&houseids=' . $houseids,
		);
		$this->_viewer->act = $act;		
		$this->_viewer->part = $part;
		$this->_setLayoutFile('ajax');
		$this->_setLayoutType('ajax');
	}
	
	function houseinfo(){
		list($houseid) = GP(array('houseid'));
		!(int)$houseid && Showmsg("data_error");
		$houseSaleModule = $this->_houseSaleHelper->getHouseSaleModule($houseid,1,1);
		(!$houseSaleModule->subject || !$houseSaleModule->username) && Showmsg("房源不存在");
		if($houseSaleModule->state == 1){
			Error::showError("对不起，该房源已下架");
		}elseif($houseSaleModule->state == 2){
			Error::showError("对不起，该房源已过期");
		}
		$saleService = $this->_houseSaleHelper->getHouseSaleService();
		$imageService = $this->_houseSaleHelper->getSecondImageService();
		$brokerHelper = House::LoadClass('brokerhelper', 'helper');
		$browselog = str_replace(",$houseid,", ',', GetCookie('browsesalelog'));
		if ($browselog  && ($log = explode(',', trim($browselog, ',')))) {
			$this->_viewer->browseSaleHouse = $saleService->getSecondInfos(array_slice($log, -6, 6));
		}
		$browselog .= ($browselog ? '' : ',') . $houseid . ',';
		substr_count($browselog, ',') > 11 && $browselog = preg_replace("/[\d]+\,/i", '', $browselog, 3);
		Cookie('browsesalelog', $browselog);
		$this->_viewer->lastUpdateHouse = $saleService->getBorkerSalesByTime($houseSaleModule->uid,$houseid,5);
		$this->_viewer->similarSaleHouse = $saleService->getSimilarList($houseid,$houseSaleModule->areatype,$houseSaleModule->price);
		$this->_viewer->secondImages = $imageService->getTopImages($houseid, 'sale', 4);
		$this->_viewer->brokerModule = $brokerHelper->getBrokerModule($houseSaleModule->uid,1);
		$this->_viewer->ifbroker = ($this->_viewer->brokerModule->uid && $this->_viewer->brokerModule->ifactive) ? true : false;
		$this->_viewer->houseid = $houseid ;
		$this->_viewer->houseSaleModule = $houseSaleModule ;
		$this->_viewer->baseUrl = $this->_baseUrl;
		$this->_viewer->showonline = $this->_getGlobal('db_showonline');
		$this->_viewer->imgpath = $this->_getGlobal('imgpath');
		$this->_viewer->modeImagePath = IMG_PATH;
		$seo = $this->_houseSaleHelper->setSeoParams('read',$houseSaleModule->subject,$houseSaleModule->areatype,$houseSaleModule->village);
		list($this->_seopage,$this->_defaultSeo,$this->_seoname) = $seo;
	}
	
	function urgent(){
		if (!isHouseManager($this->_getGlobal('winduid'),'sale')) Error::showError('您没有权限进入该页面',1); 
		$this->_viewer->baseUrl = $this->_baseUrl;
		list($houseids, $days, $isurgent, $isback, $ismsg) = $this->_gp(array('houseids', 'days', 'isurgent' ,'isback', 'ismsg'));
		if(!$houseids){
			echo '数据错误!';
			ajax_footer();
		}
		$houseids = explode(',', $houseids);
		$saleService = House::LoadService('SecondSaleService');
		$ismsg = $isurgent ? 1 : $ismsg;
		if($saleService->setUrgent($houseids, $isurgent, $days, $isback, $ismsg)){ //加急处理
			echo "操作成功!\treload";
			ajax_footer();
		}
	}
	
	function top(){
		if (!isHouseManager($this->_getGlobal('winduid'),'sale')) Error::showError('您没有权限进入该页面',1); 
		$this->_viewer->baseUrl = $this->_baseUrl;		
		list($houseids, $days, $istop, $isback, $ismsg) = $this->_gp(array('houseids', 'days', 'istop' ,'isback', 'ismsg'));
		if(!$houseids){
			echo '数据错误!';
			ajax_footer();
		}
		$houseids = explode(',', $houseids);
		$saleService = House::LoadService('SecondSaleService');
		$ismsg = $istop ? 1 : $ismsg;
		if($saleService->setTop($houseids, $istop, $days, $isback, $ismsg)){; //置顶处理		
			echo "操作成功!\treload";
			ajax_footer();
		}
	}
	
	function delete(){
		if (!isHouseManager($this->_getGlobal('winduid'),'sale')) Error::showError('您没有权限进入该页面',1); 
		list($houseids) = $this->_gp(array('houseids'));
		if(!$houseids){
			echo '数据错误!';
			ajax_footer();
		}
		$houseids = explode(',', $houseids);
		$saleService = House::LoadService('SecondSaleService');
		if($saleService->delete($houseids)){;
			echo "删除成功!\treload";
			ajax_footer();
		}
	}
	
	function report(){
		global $winduid,$windid,$timestamp;
		list($houseids,$reason,$ifsendmsg) = $this->_gp(array('houseids','reason','ifsendmsg'));
		if(!trim($reason)){
			echo "举报内容不能为空!";
			ajax_footer();
		}
		$houseReportService =  $this->_houseSaleHelper->getReportService();
		if($houseReportService->checkReport($houseids,'sale')){
			echo "此房源已被举报,已在处理中";
			ajax_footer();
		}
		$data = array(
			'uid'=>$winduid,
			'username'=>$windid,
			'reason'=>$reason,
			'handle'=>0,
			'type'=>0,
			'id'=>$houseids,
			'time'=>$timestamp
		);
		$houseReportService->add($data);
		echo "举报成功!\treload";
		ajax_footer();
	}
	
	function _getSortList(){
		return array(
			'updatetime,DESC'=>'按更新时间',
			'posttime,DESC'=>'按发布时间',
			'unitprice,ASC'=>'按单价从低到高',
			'unitprice,DESC'=>'按单价从高到低',
			'price,ASC'=>'按总价从低到高',
			'price,DESC'=>'按总价从高到低'
		);
	}
	
	function _after(){
		houseSeoSettings($this->_seopage,$this->_defaultSeo,$this->_seoname);
	}	
}